﻿using Estsh.Core.Model.EnumUtil;
using Estsh.Core.Models;
using Estsh.Core.Quartz.BaseService;
using Estsh.Core.Util;
using Estsh.Core.Wms.IRepositories;

namespace Estsh.Core.Wms.Jobs
{
    /// <summary>
    /// 处理成品下线报工业务
    /// </summary>
    public class OutPdlineJob : IJobService
    {
        private readonly IOutPdlineRepository repository;
        private readonly IPdlineRepository pdlineRepository;
        private readonly IBomRepository bomRepository;

        public OutPdlineJob(IOutPdlineRepository _repository, 
            IPdlineRepository _pdlineRepository,
            IBomRepository _bomRepository)
        {
            this.repository = _repository;
            this.pdlineRepository = _pdlineRepository;
            this.bomRepository = _bomRepository;

        }

        public string ExecuteService(string parameter)
        {
            try
            {
                List<MesWmsOutPdline> wmsRetrs = repository.GetFinishedLineOffInfo();
                if (wmsRetrs.Count > 0)
                {
                    repository.InsertFinishedLineOffInfo(wmsRetrs); // "没有待处理的成品下线数据！";
                }

                List<MesOutPdline> outPdlines = repository.GetOutPdlineInfo();
                if (outPdlines.Count <= 0)
                {
                    return "没有待报工数据！";
                }
                List<SysPdline> pdlines = pdlineRepository.GetPdlineInfo();

                foreach (var outPdline in outPdlines)
                {
                    List<SysBomDetail> bomDetails = bomRepository.GetReportBomInfo(outPdline.PartNo);
                    if (bomDetails.Count <= 0)
                    {
                        repository.UpdateOutPdlineInfo(outPdline, MesEnumUtil.OutPdlineReportStatus.EXCEPTION, "找不到BOM信息！");
                        continue;
                    }
                    var pdline = pdlines.Where(a => a.PdlineCode == outPdline.PdlineCode).FirstOrDefault();
                    if (outPdline.ProductType == (int)MesEnumUtil.OutPdlineProductType.FINISHED_PRODUCT &&
                        (pdline == null || string.IsNullOrEmpty(pdline.DestLocateName)))
                    {
                        repository.UpdateOutPdlineInfo(outPdline, MesEnumUtil.OutPdlineReportStatus.EXCEPTION, "成品入库找不到产线默认的入库库位！");
                        continue;
                    }
                    if (outPdline.ProductType == (int)MesEnumUtil.OutPdlineProductType.WIP &&
                        (pdline == null || string.IsNullOrEmpty(pdline.DestLocateName)))
                    {
                        repository.UpdateOutPdlineInfo(outPdline, MesEnumUtil.OutPdlineReportStatus.EXCEPTION, "半成品入库找不到产线默认的入库库位！");
                        continue;
                    }
                    repository.DoOutPdline(outPdline, bomDetails, pdline);
                }
                return "成品下线报工成功！";

            }
            catch (Exception ex) 
            {
                LogHelper.Error("成品下线报工错误！" + ex);
                return "成品下线报工错误！" + ex.Message;
            }
            
        }
    }
}
